//
//  Date(Extension).swift
//  HHSwift
//
//  Created by qk-huahong on 2019/5/17.
//  Copyright © 2019 huahong. All rights reserved.
//

import Foundation

extension Date{
    
    
    /// 年
    var year: Int {
        
        return Calendar.current.component(.year, from: self)
    }
    
    
    /// 月
    var month : Int {
        return Calendar.current.component(.month, from: self)
    }
    
    
    /// 日
    var day: Int {
        
        return Calendar.current.component(.day, from: self)
    }
    
    
    /// 时
    var hour : Int {
        return Calendar.current.component(.hour, from: self)
    }
    
    /// 分
    var minute: Int {
        
        return Calendar.current.component(.minute, from: self)
    }
    
    
    /// 秒
    var second : Int {
        return Calendar.current.component(.second, from: self)
    }
    
    
    /// 纳秒
    var nanosecond: Int {
        
        return Calendar.current.component(.nanosecond, from: self)
    }
    
    
    ///周几 [1~7:周日~周六]
    var weekday : Int {
        
        let arr = [nil,7,1,2,3,4,5,6]
        let index = Calendar.current.component(.weekday, from: self)
        
        return arr[index] ?? 0
    }
    
    ///
    var weekdayOrdinal: Int {
        
        return Calendar.current.component(.weekdayOrdinal, from: self)
    }
    
    
    ///本月中的第几周
    var weekOfMonth : Int {
        return Calendar.current.component(.weekOfMonth, from: self)
    }
    
    
    ///一年中的第几周
    var weekOfYear: Int {
        
        return Calendar.current.component(.weekOfYear, from: self)
    }
    
    
    ///
    var yearForWeekOfYear : Int {
        return Calendar.current.component(.yearForWeekOfYear, from: self)
    }
    
    ///
    var quarter: Int {
        
        return Calendar.current.component(.quarter, from: self)
    }
    
    
    
    /// 是否是今天
    var isToday: Bool {
        
        if fabs(self.timeIntervalSinceNow) >= 24 * 60 * 60 {
            return false
        }
        
        return Date().year == self.year
    }
    
    ///本月天数
    var daysInMonth: Int {
        let calendar = Calendar.current
        let daysInMonth = calendar.range(of: .day, in: .month, for: self)
      
        return daysInMonth?.count ?? 0
    }
    
    /// 是否是昨天
    var isYesterday: Bool {
        
        let yesterday = self.dateByAddingDays(days: 1)
        return yesterday!.isToday
    }
    
    /// 增加几N年
    ///
    /// - Parameter years: 增加的年数
    /// - Returns: 返回一个新的日期
    func dateByAddingYears(years:Int) -> Date? {
        let calendar = Calendar.current
        var components = DateComponents()
        components.setValue(self.year + years, for: .year)
        
        return calendar.date(from: components)
        
    }
    
    /// 增加几N个月
    ///
    /// - Parameter months: 增加的月数
    /// - Returns: 返回一个新的日期
    func dateByAddingMonth(months:Int) -> Date? {
        let calendar = Calendar.current
        var components = DateComponents()
        components.setValue(self.month + months, for: .month)
        
        return calendar.date(from: components)
        
    }
    
    /// 增加几N天
    ///
    /// - Parameter days: 增加的天数
    /// - Returns: 返回一个新的日期
    func dateByAddingDays(days:Int) -> Date? {
        let interval:TimeInterval = self.timeIntervalSinceReferenceDate + Double(86400 * days)
        return Date(timeIntervalSinceReferenceDate: interval)
        
    }
    
    /// 增加几N小时
    ///
    /// - Parameter hours: 增加的时辰
    /// - Returns: 返回一个新的日期
    func dateByAddingHours(hours:Int) -> Date? {
        let interval:TimeInterval = self.timeIntervalSinceReferenceDate + Double(3600 * hours)
        return Date(timeIntervalSinceReferenceDate: interval)
        
    }
    
    /// 增加N分钟
    ///
    /// - Parameter minutes: 增加的分钟
    /// - Returns: 返回一个新的日期
    func dateByAddingMinutes(minutes:Int) -> Date? {
        let interval:TimeInterval = self.timeIntervalSinceReferenceDate + Double(60 * minutes)
        return Date(timeIntervalSinceReferenceDate: interval)
        
    }
    
    /// 增加N秒
    ///
    /// - Parameter seconds: 增加的秒数
    /// - Returns: 返回一个新的日期
    func dateByAddingSeconds(seconds:Int) -> Date? {
        let interval:TimeInterval = self.timeIntervalSinceReferenceDate + Double(seconds)
        return Date(timeIntervalSinceReferenceDate: interval)
    }
    
    
    /// Date转String
    ///
    /// - Parameter format: 日期格式
    /// - Returns: 日期字符串
    func string(withFormat format:String) -> String? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.locale = Locale.current
        return formatter.string(from: self)
        
    }
    
    
    /// Date转String
    ///
    /// - Parameters:
    ///   - format: 日期格式
    ///   - timeZone: 时区
    ///   - locate: 地区
    /// - Returns: 日期字符串
    func string(withFormat format:String,timeZone:TimeZone?,locate:Locale?) -> String? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        
        if let locate = locate {
            formatter.locale = locate
        }
        
        if let timeZone = timeZone {
            formatter.timeZone = timeZone
        }
        
        return formatter.string(from: self)
        
    }
    
    
    /// String转Date
    ///
    /// - Parameters:
    ///   - dateString: 日期字符串
    ///   - format: 日期格式
    /// - Returns: Date
   static func date(withString dateString:String,format:String) -> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        let date = formatter.date(from: dateString)
        
        return date
    }
    
    
    /// String转Date
    ///
    /// - Parameters:
    ///   - dateString: 日期字符串
    ///   - format: 日期格式
    ///   - timeZone: 时区
    ///   - locate: 地区
    /// - Returns: Date
  static  func date(withString dateString:String,format:String,timeZone:TimeZone?,locate:Locale?) -> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        
        if let locate = locate {
            formatter.locale = locate
        }
        
        if let timeZone = timeZone {
            formatter.timeZone = timeZone
        }
        
        let date = formatter.date(from: dateString)
        
        return date
    }
    
    
    /// 获取当前时间
    ///
    /// - Parameter dateFormat: 时间格式
    /// - Returns: 当前时间字符串
    static func currentDate(format:String) ->String{
        
        let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.timeZone = TimeZone.current//Asia/Shanghai
        
        return formatter.string(from: date)
        
    }
    
   
}

